#include <avr/interrupt.h>

#include "print.h"
#include "KS0108.h"
#include "servos.h"
#include "AtmelSerialPorts.h"



// Set ADC reference to AVCC
// Left adjust ADC result to allow easy 8 bit reading
#define ADC_MUX ((1 << REFS0) | (1 << ADLAR) )


volatile static unsigned char index;
static unsigned char buff[128];

volatile unsigned char triggered;

ISR(ADC_vect)
{
	register unsigned char v = ADCH;

	if ( ( triggered > 0xff-64 ) || ( triggered <= 64 ) )
	{
		triggered--;
	}
	else
	{
		static unsigned char old_v = 0;
		if ( ( old_v < 128 ) && ( v > 128 ) )
		{
			triggered = 64;
		}

		old_v = v;
	}

	buff[index++] = v;
	index &= 0x7f;

	if ( triggered > 0 )
	{
		ADCSRA |= (1<<ADSC); // start next conversion 
	}
}


char HandleKeyboard()
{
	if ( !SerialCOM1_DR() )
		return 0;

	char c = SerialCOM1_Read();

	if ( c >= '1' && c <= '7' )
	{
		unsigned v = ADCSRA;
		v &= ~ ( (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0) );
		v |= c-'0';
		ADCSRA = v;
	}
	else if (c == ' ' )
	{
		return 1;
	}


	return 0;
}

char dashed1[8]={ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
char dashed2[8]={ 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 };
char dashed3[8]={ 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
char dashed4[8]={ 0x01, 0x01, 0x01, 0x81, 0x03, 0x01, 0x01, 0x01 };

char *dashed_list[8] = { dashed1, dashed2, dashed1, dashed2, dashed1, dashed2, dashed1, dashed4 };

void DrawBuffer( unsigned char *data )
{
	unsigned char vv = buff[index]/4;

	for (int i=0;i<128;i++)
	{
		unsigned char v = buff[ ( index + i ) & 0x7f ]/4;
		if ( v>vv )
		{
			KS0108_DrawVerticalLineWithBackground( i, vv, v, dashed_list[i&7] );
		}
		else
		{
			KS0108_DrawVerticalLineWithBackground( i, v, vv, dashed_list[i&7] );
		}

		vv=v;
	}
}

void Oscillo()
{
	//ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Set ADC prescalar to 128 - 125KHz sample rate @ 16MHz

	ADCSRA |= /*(1 << ADPS2);// | */(1 << ADPS1) /*| (1 << ADPS0)*/; // Set ADC prescalar to 128 - 125KHz sample rate @ 16MHz

	ADCSRA |= (1 << ADEN);  // Enable ADC
	ADCSRA |= (1 << ADIE);  // Enable ADC Interrupt

	DDRF = 0x00;
	PORTF = 0x00;

	

	for(;;)
	{
		index=0;
		triggered = 0xff;
		ADMUX = ADC_MUX; 
		ADCSRA |= (1 << ADEN); // Enable A2D Conversions 
		ADCSRA |= (1 << ADSC); // start next conversion 

		
		while( triggered > 0 )
		{
			HandleKeyboard();

			for (int i=0;i<8;i++)
				KS0108_DrawVerticalLine( i, 0, buff[0]>>2 );
		}

		DrawBuffer( buff );

	}
}
/*

volatile unsigned char adc[8];
volatile static unsigned char index;

ISR(ADC_vect)
{
	if ( index < 8)
	{
		adc[index] = ADCH;
		index++;
		if (index < 8 )
		{
			ADMUX = ADC_MUX + index;
			ADCSRA |= (1<<ADSC); // start next conversion 
		}
		else
		{
			ADMUX = ADC_MUX + 0;
			ADCSRA &= ~(1 << ADEN);  // stop A2D Conversions 		
		}
	}
}


unsigned char local_adc[8];

void OscilloPrint()
{
	for (char i=0;i<8;i++)
	{
		PrintHex8( local_adc[i] );
		Device_PrintChar('-');
	}
	Device_PrintCRLF();
}

void OscilloBars()
{
	for (int i=0;i<128;i++)
	{
		KS0108_DrawVerticalLine( i, 0, local_adc[i>>3]>>2 );
	}
}

void Oscillo()
{
	ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Set ADC prescalar to 128 - 125KHz sample rate @ 16MHz

	ADCSRA |= (1 << ADEN);  // Enable ADC
	ADCSRA |= (1 << ADIE);  // Enable ADC Interrupt

	DDRF = 0x00;
	PORTF = 0x00;

	for(;;)
	{
		index=0;
		ADMUX = ADC_MUX; 
		ADCSRA |= (1 << ADEN); // Enable A2D Conversions 
		ADCSRA |= (1 << ADSC); // start next conversion 
		
		while( index<8 );

		for (char i=0;i<8;i++)
		{
			local_adc[i]=adc[i];
		}

		OscilloBars();
	}
}

*/
